package com.edu.cdp.controller;

import com.edu.cdp.DTO.PageDTO;
import com.edu.cdp.VO.OrderPaymentVO;
import com.edu.cdp.VO.OrderVO;
import com.edu.cdp.common.PageResult;
import com.edu.cdp.common.Result;
import com.edu.cdp.domain.Orders;
import com.edu.cdp.service.OrdersService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/order")
@Tag(name="订单相关接口")
@Slf4j
public class OrderController {
    @Autowired
    OrdersService ordersService;

    @GetMapping("/list")
    @Operation(summary = "获取订单列表")
    public Result<List<Orders>> list() {
        List<Orders> list = ordersService.list();
        if (list.size() > 0) {
            return Result.success(list);
        } else {
            return Result.error("订单列表为空");
        }
    }

    @GetMapping("page")
    @Operation(summary = "分页查询订单列表")
    public Result<PageResult> page(@RequestBody PageDTO pageDTO) {
        PageResult list = ordersService.pageQuery(pageDTO);
        if (list != null) {
            return Result.success(list);
        } else {
            return Result.error("订单列表为空");
        }
    }

    @GetMapping("/listByUserId")
    @Operation(summary = "根据用户id查询订单")
    public Result<List<OrderVO>> listByUserId(
            @Parameter(name = "id", description = "用户id")
                                                 Integer id) {
        List<OrderVO> list = ordersService.getByUserId(id);

        if (list.size() > 0){
            return Result.success(list);
        } else {
            return Result.error("该用户暂无订单");
        }
    }

    @GetMapping("/listNotPaid")
    @Operation(summary = "查询用户未支付订单")
    public Result<List<Orders>> listNotPaid(
            @Parameter(name = "id", description = "用户id")
                                                Integer id) {
        List<Orders> list = ordersService.getByNotPaid(id);

        if (list.size() > 0){
            return Result.success(list);
        } else {
            return Result.error("该用户无未支付订单");
        }
    }

    @GetMapping("/listByPay")
    @Operation(summary = "查询用户已支付订单")
    public Result<List<Orders>> listByPay(
            @Parameter(name = "id", description = "用户id")
                                              Integer id) {
        List<Orders> list = ordersService.getByPay(id);

        if (list.size() > 0){
            return Result.success(list);
        } else {
            return Result.error("该用户无已支付订单");
        }
    }

    @GetMapping("/listByTimeInterval")
    @Operation(summary = "通过时间区间查询订单")
    public Result<List<Orders>> listByTime(@Schema(description = "开始时间",
            example = "2024-05-28") String startTime,
                           @Schema(description = "结束时间", example = "2024-05-29")
                                           String endTime) {
        List<Orders> list = ordersService.getByTimeInterval(startTime, endTime);

        if (list.size() > 0) {
            return Result.success(list);
        } else {
            return Result.error("该日期区间无订单");
        }
    }

    @GetMapping("/listByTime")
    @Operation(summary = "通过日期获取订单")
    public Result<List<Orders>> listByTime(@Parameter(name = "id", description = "用户id")
                                           Integer id,
                                           @Schema(description = "日期", example = "2024-05-29")String time){
        System.out.println("前端请求："+ time);
        List<Orders> list = ordersService.getByTime(id, time);

        if (list.size() == 0) {
            return Result.error("该用户当天无订单");
        } else {
            return Result.success(list);
        }
    }

    @GetMapping("/detail")
    @Operation(summary = "获取订单详情")
    public Result<OrderVO> orderDetail(
            @Parameter(name = "id", description = "订单id")
                                       Integer id){
        OrderVO detail =  ordersService.getDetails(id);
        if (detail == null) {
            return Result.error("查无此订单");
        } else {
            return Result.success(detail);
        }
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除订单")
    public Result<String> delete(
            @Parameter(name = "id", description = "订单id")
            Integer id) {
        boolean result = ordersService.delete(id);
        if (result) {
            return Result.success();
        } else {
            return Result.error("删除失败");
        }
    }

    @PutMapping("/payment")
    @Operation(summary = "支付订单")
    public Result<OrderPaymentVO> payment(
            @Parameter(name = "orderId", description = "订单id")
                                              Integer orderId) {
        log.info("订单id：{}", orderId);
        OrderPaymentVO paymentVO = ordersService.getAmount(orderId);
        if (paymentVO == null) {
            return Result.error("余额不足");
        } else {
            return Result.success(paymentVO);
        }
    }

    @PutMapping("/cancel")
    @Operation(summary = "取消订单")
    public Result<Orders> cancel(
            @Parameter(name = "id", description = "订单id")
            Integer id) {
        Orders order = ordersService.cancel(id);

        if (order == null) {
            return Result.error("操作失败");
        } else {
            return Result.success(order);
        }
    }

    @PostMapping("/add")
    @Operation(summary = "添加订单")
    public Result<Orders> add(@Parameter(name = "id", description = "用户id")
                                  Integer id, @Parameter(name = "staffId", description = "员工id")
    Integer staffId) {
        Orders order = ordersService.add(id, staffId);

        if (order == null) {
            return Result.error("添加失败");
        } else {
            return Result.success(order);
        }
    }

    @PutMapping("/refund")
    @Operation(summary = "退单")
    public Result<Orders> refund(
            @Parameter(name = "id", description = "订单id")
                                     Integer id) {
        Orders order = ordersService.refund(id);

        if (order == null) {
            return Result.error("退单失败");
        } else {
            return Result.success(order);
        }
    }

    @GetMapping("/cancelled")
    @Operation(summary = "查询已取消订单")
    public Result<List<Orders>> cancelled() {
        List<Orders> orders = ordersService.cancelled();

        return Result.success(orders);
    }

    @GetMapping("/orderByStatus")
    @Operation(summary = "根据状态查询用户订单")
    public Result<List<Orders>> orderByStatus(
            @Parameter(name = "id", description = "用户id")
            Integer id,
            @Parameter(name = "id",
                    description = "订单状态，0表示已取消，1表示正常" )
            Integer status) {
        List<Orders> list = ordersService.getByStatus(id, status);

        return Result.success(list);
    }
}
